#!/usr/bin/env python2
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

import sys
import cli_urlpassgen

# close if Ctrl+C is pressed
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(360, 262)
        
        self.verticalLayout = QtGui.QVBoxLayout(Form)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        
        spacerItem = QtGui.QSpacerItem(14, 5, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        self.verticalLayout.addItem(spacerItem)

        self.label = QtGui.QLabel(Form)
        self.label.setMinimumSize(QtCore.QSize(0, 15))
        self.label.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft)
        self.label.setObjectName(_fromUtf8("label"))

        self.verticalLayout.addWidget(self.label)
        
        self.urlEdit = QtGui.QLineEdit(Form)
        self.urlEdit.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.urlEdit.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.urlEdit.setText(_fromUtf8(""))
        self.urlEdit.setFrame(True)
        self.urlEdit.setEchoMode(QtGui.QLineEdit.Normal)
        self.urlEdit.setAlignment(QtCore.Qt.AlignCenter)
        self.urlEdit.setObjectName(_fromUtf8("urlEdit"))
        
        self.verticalLayout.addWidget(self.urlEdit)
        
        spacerItem1 = QtGui.QSpacerItem(18, 15, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
        
        self.verticalLayout.addItem(spacerItem1)
        
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setFrameShadow(QtGui.QFrame.Plain)
        self.label_2.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        
        self.verticalLayout.addWidget(self.label_2)
        
        self.passEdit = QtGui.QLineEdit(Form)
        self.passEdit.setText(_fromUtf8(""))
        self.passEdit.setEchoMode(QtGui.QLineEdit.Password)
        self.passEdit.setAlignment(QtCore.Qt.AlignCenter)
        self.passEdit.setObjectName(_fromUtf8("passEdit"))
        
        self.verticalLayout.addWidget(self.passEdit)
        
        spacerItem2 = QtGui.QSpacerItem(18, 15, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
        
        self.verticalLayout.addItem(spacerItem2)
        
        self.label_3 = QtGui.QLabel(Form)
        self.label_3.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        
        self.verticalLayout.addWidget(self.label_3)
        
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        
        self.genpassEdit = QtGui.QLineEdit(Form)
        self.genpassEdit.setFocusPolicy(QtCore.Qt.NoFocus)
        self.genpassEdit.setAlignment(QtCore.Qt.AlignCenter)
        self.genpassEdit.setReadOnly(True)
        self.genpassEdit.setObjectName(_fromUtf8("genpassEdit"))
        
        self.horizontalLayout.addWidget(self.genpassEdit)
        
        spacerItem3 = QtGui.QSpacerItem(20, 13, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
        
        self.horizontalLayout.addItem(spacerItem3)
        
        self.genButton = QtGui.QPushButton(Form)
        self.genButton.setObjectName(_fromUtf8("genButton"))
        
        self.horizontalLayout.addWidget(self.genButton)
        
        self.verticalLayout.addLayout(self.horizontalLayout)
        
        spacerItem4 = QtGui.QSpacerItem(14, 5, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        
        self.verticalLayout.addItem(spacerItem4)

        self.retranslateUi(Form)
        #QtCore.QObject.connect(self.genButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.genpassEdit.selectAll)
        QtCore.QObject.connect(self.genButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.set_pass)
        QtCore.QObject.connect(self.urlEdit, QtCore.SIGNAL(_fromUtf8("returnPressed()")), self.passEdit.setFocus)
        QtCore.QObject.connect(self.passEdit, QtCore.SIGNAL(_fromUtf8("returnPressed()")), self.genButton.setFocus)
        QtCore.QMetaObject.connectSlotsByName(Form)
    
    def set_pass(self):
        self.genpassEdit.setText(_fromUtf8(cli_urlpassgen.pass_gen(self.urlEdit.text(), 
                                                        self.passEdit.text())))
        self.genpassEdit.selectAll()
        # needs pass copy to clipboard

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "PassGen", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Form", "  Site Addres:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Form", "  Master Password:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("Form", "  Site Password:", None, QtGui.QApplication.UnicodeUTF8))
        self.genButton.setText(QtGui.QApplication.translate("Form", "Generate", None, QtGui.QApplication.UnicodeUTF8))


def urltranc(url):
    """Parse a URL into 3 components:
    <scheme>://<netloc>/netloc/<path_fragm>
    Return a 6-tuple: (scheme, netloc, path_fragm."""

    uses_scheme = ['http', 'https', 'shttp', 'ftp', 'sftp']
    sep = '://'
    url_trunc = ''
    print '---' + url + '---'
    if not url.find(sep) is '-1':
        url_tuple = url.split(sep)
        scheme = url_tuple[0]
        if scheme in uses_scheme:
            url_trunc = scheme + sep  + url_tuple[1].split('/')[0]
    return url_trunc


def main():
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    ###
    last_clip = str(app.clipboard().text().toUtf8())
    url = urltranc(last_clip)
    if url:
        ui.urlEdit.setText(_fromUtf8(url))
        ui.passEdit.setFocus()
    ###
    Form.show()
    
    # close on hide
    app.connect(app, QtCore.SIGNAL('hide()'), app, QtCore.SLOT('quit()'))
    Form.connect(Form, QtCore.SIGNAL('hide()'), app, QtCore.SLOT('quit()'))

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()